const http = require("http");
const url = require('url')
const app = http.createServer();

const { handleSpider } = require("./middleware/spider");
const { getList } = require("./controllers/spider");
app
	.on("request", async (req, res) => {
		// console.log(req.method);
		// console.log(req.url);
		// req.method 请求方式
		// req.url 请求完整路径
		// 请求完整信息
		const parseObj = url.parse(req.url, true);
		// 请求路径
		const { pathname } = parseObj
		if (pathname == "/spider/createSpider") {
			// 请求参数解构
			const searchUrl = parseObj.query.url
			// console.log(parseObj.query.url);
			let data = await handleSpider(searchUrl);


			let result = {
				state: 0,
				data: data,
			};
			res.writeHead(200, { "Content-Type": "application/json;charset=utf-8" });
			res.end(JSON.stringify(result));
		}

		if (pathname == "/spider/getList") { getList(req, res) }
	})
app.listen(3000, () => {
	console.log("listen 3000 ...");
});
